package cn.ztelovecly.springboot.framework.util;

import java.net.InetAddress;

public class SystemUtil {

    public static long getThreadId(){
        Thread current = Thread.currentThread();
        return current.getId();
    }

    public static String getHostIp(){
        try {
            InetAddress addr = InetAddress.getLocalHost();
            return(addr.getHostAddress());//获得本机IP
        }catch(Exception ex){
            return "";
        }
    }

    public static String getHostName(){
        try {
            InetAddress addr = InetAddress.getLocalHost();
            return(addr.getHostName());//获得本机IP
        }catch(Exception ex){
            return "";
        }
    }

    /**
     * 使用线程堆栈信息获取方法的调用者类名
     * @return 调用者类名
     */
    public static String getCallerClassName(int level){
        StackTraceElement stack[] = Thread.currentThread().getStackTrace();
        String className;
        try {
            className = stack[level].getClassName();
        }catch(Exception ex){
            ex.printStackTrace();
            className = "";
        }
        return className;
    }

    /**
     * 使用线程堆栈信息获取方法的调用者类名
     * @return 调用者类名
     */
    public static String getCallerMethodName(int level){
        StackTraceElement stack[] = Thread.currentThread().getStackTrace();
        String methodName;
        try {
            methodName = stack[level].getMethodName();
        }catch(Exception ex){
            ex.printStackTrace();
            methodName = "";
        }
        return methodName;
    }
}
